import { useEffect, useState } from 'react';
import { Table, Button, Tooltip, Space, message } from 'antd';
import { orderManagement } from '../../../../apis/auth';
import PropTypes from 'prop-types';

const OrderManagement1 = ({ filterConditions }) => {
    const [data, setData] = useState([]);
    const [loading, setLoading] = useState(false);
    const [current, setCurrent] = useState(1); // 设置默认显示的开始页数当前页数
    const [pageSize, setPageSize] = useState(5); // 设置默认显示每页显示为5条
    const [total, setTotal] = useState(0); // 订单总数

    useEffect(() => {
        fetchData();
    }, [current, pageSize, filterConditions]);

    const fetchData = async () => {
        setLoading(true);
        try {
            const params = {
                current,
                pageSize,
                //将header.jsx中的参数传递过来，并且进行解构
                ...filterConditions,
            };
            console.log('Request Params:', params); // 添加日志打印
            const response = await orderManagement.searchOrder(params);
            if (response.data.code === 201) {
                setData(response.data.data.list);
                setTotal(response.data.data.total);
            } else {
                message.error(response.data.message);
            }
        } catch (error) {
            message.error('请求失败，稍后再试');
        } finally {
            setLoading(false);
        }
    };

    const columns = [
        {
            title: '订单号',
            dataIndex: 'orderId',
            key: 'orderId',
        },
        {
            title: '用户姓名',
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: '用户电话',
            dataIndex: 'phone',
            key: 'phone',
        },
        {
            title: '下单时间',
            dataIndex: 'buyTime',
            key: 'buyTime',
        },
        {
            title: '备注',
            dataIndex: 'notes',
            key: 'notes',
        },
        {
            title: '订单状态',
            dataIndex: 'orderStatus',
            key: 'orderStatus',
        },
        {
            title: '操作',
            key: 'action',
            render: (_, record) => (
                <Space size="middle">
                    <Tooltip title="发货">
                        <Button
                            type="primary"
                            onClick={() => handleSend(record.orderId)}
                        >
                            发货
                        </Button>
                    </Tooltip>
                </Space>
            ),
        },
    ];
    // const handleSend = (orderId) => {
    //     // 发货逻辑
    // };


    return (
        <>
            <Table
                columns={columns}
                dataSource={data}
          
                rowKey="orderId"
                //分页器通过传递page和pageSize参数来控制数据的显示
                pagination={{
                    current: current,
                    pageSize: pageSize,
                    total: total,
                    showSizeChanger: true,
                    onChange: (page, pageSize) => {
                        setCurrent(page);
                        setPageSize(pageSize);
                    },
                }}
                loading={loading}    
            />
        </>
    );
};

OrderManagement1.propTypes = {
    filterConditions: PropTypes.object
};

export default OrderManagement1;
